package ebedynky.services;

import ebedynky.beans.HibernateUtil;
import ebedynky.beans.Box;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 * @author davidon1
 */
public class BoxService {

    /**
     * Seesion - current connection to the db.
     */
    private Session session;

    /**
     * Constructor, retrieces the session (connection).
     */
    public BoxService() {
        this.session = HibernateUtil.getSessionFactory().openSession();
    }

    /**
     * Closes the session (connection).
     * @return success/failure.
     */
    public boolean close() {
        this.session.close();
        return !(this.session.isOpen());
    }

    /**
     * Restarts the session (connection).
     * @return success/failure.
     */
    public boolean restart() {
        this.session.close();
        if (this.session.isOpen()) {
            throw new IllegalStateException("Session could not be closed.");
        }
        this.session = HibernateUtil.getSessionFactory().openSession();
        return this.session.isOpen();
    }

    /**
     * Adds box to the db.
     * @param b Box to be inserted
     * @return ID of the added box
     */
    public int addBox(Box b) {
        session.beginTransaction();
        int id = getMaxBoxID() + 1;
        b.setBoxID(id);
        id = (Integer) session.save(b);
        session.getTransaction().commit();
        return (id);
    }

    /**
     * Retrieves the maximum id currently in the database of boxes.
     * @return Max ID
     */
    public int getMaxBoxID() {
        String hql = "select max(b.boxID) from Box b";
        List list = session.createQuery(hql).list();
        if (list.get(0) != null) {
            Integer maxbid = (Integer) list.get(0);
            return maxbid;
        } else {
            return 0;
        }
    }

    /**
     * Retrieves a box identified by the ID.
     * @param id of the Box
     * @return Box with the ID given (null if not found)
     */
    public Box getBoxByID(int id) {
        session.beginTransaction();
        Box b = (Box) session.get(Box.class, new Integer(id));
        session.getTransaction().commit();
        return (b);
    }

    /**
     * Retrieves a list of all boxs stored in the db.
     * @return List of all Boxes (null if no boxs)
     */
    public List<Box> getAllBoxes() {
        session.beginTransaction();
        List<Box> bList = session.createQuery("from Box").list();
        session.getTransaction().commit();
        return (bList);
    }

    /**
     * Updates a specified box identified by ID.
     * @param newBox an updated box
     */
    public void updateBox(Box newBox) {
        session.beginTransaction();
        Box b = (Box) session.get(Box.class, new Integer(newBox.getBoxID()));
        b.setBoxorders(newBox.getBoxorders());
        b.setOfferitems(newBox.getOfferitems());
        b.setOffer(newBox.getOffer());
        b.setPrice(newBox.getPrice());
        b.setQuantity(newBox.getQuantity());
        session.flush();
        session.getTransaction().commit();
        session.clear();
    }

    /**
     * Removes the Box from db.
     * @param id Box ID for removal
     * @return true if successful, false otherwise
     */
    public boolean deleteBox(int id) {
        try {
            session.beginTransaction();
            Box b = (Box) session.get(Box.class, new Integer(id));
            session.delete(b);
            session.getTransaction().commit();
            return (true);
        } catch (HibernateException ex) {
            return (false);
        }
    }
}